package com.school.campus.service.impl;

import java.util.List;

import com.school.campus.domain.Conversations;
import com.school.campus.mapper.ConversationsMapper;
import com.school.common.utils.DateUtils;
import com.school.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.school.campus.mapper.MessagesMapper;
import com.school.campus.domain.Messages;
import com.school.campus.service.IMessagesService;

/**
 * 单聊消息（存储一对一文字消息）Service业务层处理
 *
 * @author wwj
 * @date 2025-11-07
 */
@Service
public class MessagesServiceImpl implements IMessagesService {
    @Autowired
    private MessagesMapper messagesMapper;


    @Autowired
    private ConversationsMapper conversationsMapper;

    /**
     * 查询单聊消息（存储一对一文字消息）
     *
     * @param id 单聊消息（存储一对一文字消息）主键
     * @return 单聊消息（存储一对一文字消息）
     */
    @Override
    public Messages selectMessagesById(Long id) {
        return messagesMapper.selectMessagesById(id);
    }

    /**
     * 查询单聊消息（存储一对一文字消息）列表
     *
     * @param messages 单聊消息（存储一对一文字消息）
     * @return 单聊消息（存储一对一文字消息）
     */
    @Override
    public List<Messages> selectMessagesList(Messages messages) {
        Long userId = SecurityUtils.getUserId();
        Conversations conversations = conversationsMapper.selectConversationsById(messages.getConversationId());
        if (userId.equals(conversations.getUser1Id())) {
            conversations.setUnreadCount1(0L);
        }else {
            conversations.setUnreadCount2(0L);
        }
        conversationsMapper.updateConversations(conversations);
        return messagesMapper.selectMessagesList(messages);
    }

    /**
     * 新增单聊消息（存储一对一文字消息）
     *
     * @param messages 单聊消息（存储一对一文字消息）
     * @return 结果
     */
    @Override
    public int insertMessages(Messages messages) {
        messages.setSendTime(DateUtils.getNowDate());
        int i = messagesMapper.insertMessages(messages);
        Conversations conversations = conversationsMapper.selectConversationsById(messages.getConversationId());
        Long userId = SecurityUtils.getUserId();
        if (userId.equals(conversations.getUser1Id())) {
            conversations.setUnreadCount2(conversations.getUnreadCount2() + 1L);
        }else {
            conversations.setUnreadCount1(conversations.getUnreadCount1() + 1L);
        }
        conversations.setLatestMsgId(messages.getId());
        conversations.setLatestMsgTime(messages.getSendTime());
        conversationsMapper.updateConversations(conversations);
        return i;
    }

    /**
     * 修改单聊消息（存储一对一文字消息）
     *
     * @param messages 单聊消息（存储一对一文字消息）
     * @return 结果
     */
    @Override
    public int updateMessages(Messages messages) {
        return messagesMapper.updateMessages(messages);
    }

    /**
     * 批量删除单聊消息（存储一对一文字消息）
     *
     * @param ids 需要删除的单聊消息（存储一对一文字消息）主键
     * @return 结果
     */
    @Override
    public int deleteMessagesByIds(Long[] ids) {
        return messagesMapper.deleteMessagesByIds(ids);
    }

    /**
     * 删除单聊消息（存储一对一文字消息）信息
     *
     * @param id 单聊消息（存储一对一文字消息）主键
     * @return 结果
     */
    @Override
    public int deleteMessagesById(Long id) {
        return messagesMapper.deleteMessagesById(id);
    }

    @Override
    public int clear(Long conversationId) {
        return messagesMapper.deleteMessagesByConversationId(conversationId);
    }
}
